package com.neusoft.a132wxyorderingdish.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.neusoft.a132wxyorderingdish.R;
import com.neusoft.a132wxyorderingdish.activity.FoodListActivity;
import com.neusoft.a132wxyorderingdish.beans.ShopBean;
import com.neusoft.a132wxyorderingdish.common.Constants;
import com.squareup.picasso.Picasso;

import java.util.List;

public class ShopAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<ShopBean>mDatalist;
    private LayoutInflater mLayoutInflater;
    public ShopAdapter(Context mContext,List mDatalist){
        this.mContext = mContext;
        this.mDatalist = mDatalist;
        mLayoutInflater = LayoutInflater.from(mContext);
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup,int i){
        View v = mLayoutInflater.from(viewGroup.getContext()).inflate(R.layout.shop_item,viewGroup,false);
        return  new ViewHolder(v);
    }
    @Override
    public void  onBindViewHolder(RecyclerView.ViewHolder holder,int position){
        final ShopBean entity = (mDatalist.get(position));
        if(null == entity)
            return;
        ViewHolder viewHolder =(ViewHolder)holder;
        viewHolder.res_name.setText(entity.getShopname());
        viewHolder.res_address.setText(entity.getAddress());
        viewHolder.res_bar.setRating(Float.parseFloat(entity.getLevel()));
        Picasso.get().load(Constants.BASE_URL+entity.getPic()).into(viewHolder.image);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(mContext,FoodListActivity.class);
                intent.putExtra("shop_id",entity.getShop_id());
                intent.putExtra("shop_name",entity.getShopname());
                mContext.startActivity(intent);
            }
        });
    }
    @Override
    public int getItemCount(){
        if (mDatalist == null){

            return 0;
        }
        return mDatalist.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        public ImageView image;
        RatingBar res_bar;
        TextView res_name,res_address;

        public ViewHolder(View view){
            super(view);
            res_name = (TextView)view.findViewById(R.id.res_name);
            res_bar = (RatingBar)view.findViewById(R.id.ratingBar1);
            image = (ImageView)view.findViewById(R.id.res_image);
            res_address = (TextView)view.findViewById(R.id.res_address);

        }
    }
}
